// Category
DLSS.Category = Ext.data.Record.create([
    {name: 'id'},
    {name: 'name', type: 'string'}
]);

DLSS.categoriesReader = new Ext.data.XmlReader({record:'category'},
	['id', 'name'], DLSS.Category);

DLSS.categoriesStore = new Ext.data.Store({
    url: 'categoriesList.htm',
    reader: DLSS.categoriesReader,
    sortInfo:{field:'id', direction:'ASC'}
});

DLSS.categAdjudicatorsGrid = new Ext.grid.EditorGridPanel({
    cellCls:'categ-adjudicator-management-table',
    store: DLSS.categAdjudicatorsStore,
    cm: DLSS.adjudicatorsColumnModel,
    width:500,
    height:275,
    style:'margin-left:10px;margin-top:20px;',
    autoExpandColumn:'number',
    title:'Adjudicators',
    frame:true,
    viewConfig: {
        forceFit: true
    },
    clicksToEdit:2,
    tbar: [{
        text: 'Edit',
        handler: function() {DLSS.categAdjudicatorsModal.show();}
      }
    ]
});

DLSS.categDancesGrid = new Ext.grid.EditorGridPanel({
	cellCls:'categ-dance-management-table',
	store: DLSS.categDancesStore,
	cm: DLSS.dancesColumnModel,
	width:300,
	height:150,
	style:'margin-left:50px; margin-top:20px;',
	autoExpandColumn:'name',
	title:'Dances',
	frame:true,
	viewConfig: {
	    forceFit: true
	},
	clicksToEdit:2,
	tbar: [{
        text: 'Edit',
        handler: function() {DLSS.categDancesModal.show();}
      }
    ]
});

DLSS.categCouplesGrid = new Ext.grid.EditorGridPanel({
	cellCls:'categ-couple-management-table',
	store: DLSS.categCouplesStore,
	cm: DLSS.couplesColumnModel,
	width:300,
	height:150,
	style:'margin-left:10px;margin-top:20px;',
	autoExpandColumn:'name',
	title:'Couples',
	frame:true,
	viewConfig: {
	    forceFit: true
	},
	clicksToEdit:2,
	tbar: [{
        text: 'Edit',
        handler: function() {DLSS.categCouplesModal.show();}
      }
    ]
});
                
// Category CRUD form
DLSS.categoriesForm = new Ext.FormPanel({
	frame:true,
    border:false,
    height:250,
    defaults:{autoHeight: true},
    autoScroll:true,
    items:[
       	new Ext.form.Hidden({id:'fId', name:'id'}),
       	new Ext.form.TextField({id:'fName', fieldLabel:'Name', name:'name', labelStyle:'margin-left:10px;margin-top:10px;', style:'margin-top:10px;'}),
       	DLSS.roundsGrid,
       	DLSS.categCouplesGrid,
       	new Ext.Panel({
			frame:false,
       		header:false,
       		border:false,
       	    layout:'table',
       	    columns:2,
       	    //width:550,
		    //height:650,
		    //style:'margin-top:5px;',
		    items:[
				DLSS.categAdjudicatorsGrid,
              	DLSS.categDancesGrid
              	]
            })              
  	]
});

// Modal Category window
DLSS.categoriesModal = {
	add: function() {
		DLSS.categoriesForm.getComponent('fId').setValue('');
		DLSS.categoriesForm.getComponent('fName').setValue('');
		DLSS.categoriesModal.show(DLSS.getMessage('categories_add'), DLSS.categoriesForm);
	},
	
	edit: function(record/*sm*/) {
        //var record = sm.getSelected();
        if(record) {
			DLSS.categoriesForm.getComponent('fId').setValue(record.get('id'));
			DLSS.categoriesForm.getComponent('fName').setValue(record.get('name'));
		}
		DLSS.roundsStore.load();
	    DLSS.categoriesModal.show(DLSS.getMessage('categories_edit'), DLSS.categoriesForm);
	},
	
	show: function(title, items) {
		var win;
		// create the window on the first click and reuse on subsequent clicks
		if(!win){
		     win = new Ext.Window({
		         title: title,
		         layout:'fit',
		         modal:true,
		         border:false,
		         width:950,
		         height:700,
		         closeAction:'hide',
		         plain: true,
		         items: [items],
		         buttons: [
		         	{text:DLSS.getMessage('button_submit'), handler: function(){DLSS.categoriesModal.performAction(); win.hide();}},
		         	{text:DLSS.getMessage('button_close'), handler: function(){win.hide();}}
		         ]
		     });
		 }
		 win.show(this);    
	},
	
	performAction: function() {
  		DLSS.categoriesForm.form.submit({url: 'categoryUpdate.htm', waitMsg: 'Saving Data...', success: DLSS.categoriesModal.doneFunction});
    }, 
  
   doneFunction: function(form,action) {
    	//alert("success");
    	DLSS.categoriesStore.reload();
   }
	
};